package org.hydrakyoufeng.stuplatform.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.hydrakyoufeng.stuplatform.dto.*;
import org.hydrakyoufeng.stuplatform.service.CourseService;
import org.hydrakyoufeng.stuplatform.vo.CourseListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("course")
@Tag(name = "课程")
@Slf4j
public class CourseController {

    @Autowired
    private CourseService courseService;

    @PostMapping("queryList")
    public ResponseEntity queryList(@RequestBody CourseListDTO courseListDTO) throws Exception  {
        return ResponseEntity.ok(courseService.queryList(courseListDTO));
    }

    @GetMapping("queryListInClass/{class_id}")
    public ResponseEntity queryListInClass(@PathVariable("class_id") Integer class_id) throws Exception  {
        return ResponseEntity.ok(courseService.queryListInClass(class_id));
    }

    @PostMapping("queryListNotInClass")
    public ResponseEntity queryListNotInClass(@RequestBody CourseListDTO courseListDTO) throws Exception  {
        return ResponseEntity.ok(courseService.queryListNotInClass(courseListDTO));
    }
    @PostMapping("insert")
    public ResponseEntity insert(@RequestBody CourseDTO courseDTO) throws Exception  {
        return ResponseEntity.ok(courseService.insert(courseDTO));
    }

    @PostMapping("update")
    public ResponseEntity update(@RequestBody CourseDTO courseDTO) throws Exception  {
        return ResponseEntity.ok(courseService.update(courseDTO));
    }

    @DeleteMapping("delete/{id}")
    public ResponseEntity delete(@PathVariable("id") Integer id) throws Exception  {
        return ResponseEntity.ok(courseService.delete(id));
    }

    /**
     * 导出excel
     * @param courseListDTO
     * @param response
     * @return
     * @throws Exception
     */
    @PostMapping("getExcel")
    public void getExcel(@RequestBody CourseListDTO courseListDTO, HttpServletResponse response) throws Exception {
        String fileName = "课程列表";
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileNameEncode = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileNameEncode + ".xlsx");
        List<CourseListVO> list = courseService.getExcel(courseListDTO);
        EasyExcel.write(response.getOutputStream())
                .head(CourseListVO.class)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet(fileName)
                .doWrite(list);
    }

    /**
     * 查询用户的课程列表
     * @param courseUserListDTO
     * @return
     * @throws Exception
     */
    @PostMapping("queryUserList")
    public ResponseEntity queryUserList(@RequestBody CourseUserListDTO courseUserListDTO) throws Exception  {
        return ResponseEntity.ok(courseService.queryUserList(courseUserListDTO));
    }

    /**
     * 更新用户对于课程的操作
     * @param courseUserOperateDTO
     * @return
     * @throws Exception
     */
    @PostMapping("updateUserOperate")
    public ResponseEntity updateUserOperate(@RequestBody @Valid CourseUserOperateDTO courseUserOperateDTO) throws Exception  {
        return ResponseEntity.ok(courseService.updateUserOperate(courseUserOperateDTO));
    }


    /**
     * 前端用户插入对课程的纠错信息
     * @param dto
     * @return
     * @throws Exception
     */
    @PostMapping("insertCorrecting")
    public ResponseEntity insertCorrecting(@RequestBody @Valid CorrectingUserDTO dto) throws Exception  {
        return ResponseEntity.ok(courseService.insertCorrecting(dto));
    }

    /**
     * 管理员查看课程的纠错信息列表
     * @param dto
     * @return
     * @throws Exception
     */
    @PostMapping("queryCorrectingList")
    public ResponseEntity queryCorrectingList(@RequestBody CorrectingListDTO dto) throws Exception  {
        return ResponseEntity.ok(courseService.queryCorrectingList(dto));
    }

    /**
     * 管理员审核课程的纠错信息
     * @param dto
     * @return
     * @throws Exception
     */
    @PostMapping("updateCorrecting")
    public ResponseEntity updateCorrecting(@RequestBody @Valid CorrectingDTO dto) throws Exception  {
        return ResponseEntity.ok(courseService.updateCorrecting(dto));
    }
}
